package com.tarena.lbs.attach.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarena.lbs.attach.converter.AttachConverter;
import com.tarena.lbs.attach.dao.mapper.AttachMapper;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.enums.FileTypeEnum;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.service.AttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Mikazuki on 2025/6/9.
 */
@Service
public class AttachServiceImpl extends ServiceImpl<AttachMapper, AttachPO> implements AttachService {
    @Autowired
    private AttachConverter attachConverter;
    @Autowired
    private AttachRepository attachRepository;
    @Override
    public AttachDTO insertAttachInfo(MultipartFile file, String fileUuid) throws Exception {
        AttachPO po = new AttachPO();
        po.setClientFileName(file.getOriginalFilename());
        po.setFileUuid(fileUuid);
        po.setContentType(getFileType(file));
        po.setContentLength(file.getSize() / 1024);
        if (FileTypeEnum.PICTURE.getType().equals(po.getContentType())) {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage != null) {
                po.setWidth(bufferedImage.getWidth());
                po.setHeight(bufferedImage.getHeight());
            }
        }
        save(po);
        return attachConverter.convertModelToDto(po);
    }


    private Integer getFileType(MultipartFile file) {
        Integer fileType = FileTypeEnum.UNKNOWN.getType();
        if (file.getContentType().contains("video")) {
            fileType = FileTypeEnum.VIDEO.getType();
        } else if (file.getContentType().contains("image")) {
            fileType = FileTypeEnum.PICTURE.getType();
        }
        return fileType;
    }
    @Override
    public Boolean bathUpdateBusiness(List<PicUpdateParam> picUpdateParamList) {
        if (CollectionUtils.isEmpty(picUpdateParamList)){
            return false;
        }
        return attachRepository.batchUpdateBusiness(picUpdateParamList);
    }

    @Override
    public List<AttachDTO> batchGetAttachInfo(List<Integer> ids, Integer type) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return attachRepository.batchGetAttachInfo(ids, type);
    }

    @Override
    public AttachDTO ByIdGatAttach(String logoId) {
        return attachRepository.ByIdGatAttach(logoId);
    }

    @Override
    public AttachQrDTO generateQrCode(AttachQRParam attachQRParam) {
        if (attachQRParam != null) {
            return attachRepository.generateQrCode(attachQRParam);
        }
        return null;
    }
}
